package com.sphd.beautysalon.activitys.marking;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.sphd.beautysalon.adapters.CouponMsgAdapter;
import com.sphd.beautysalon.base.RestaurantUrl;
import com.sphd.beautysalon.info.LoginInfo;
import com.sphd.beautysalon.info.RestaurantInfo;
import com.sphd.beautysalon.presenter.CouponPresenterImpl;
import com.sphd.beautysalon.utils.Constant;
import com.sphd.beautysalon.R;
import com.sphd.beautysalon.base.BaseActivity;
import com.sphd.beautysalon.info.CouponMsgInfo;
import com.sphd.beautysalon.view.CouponView;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 促销与优惠
 */
public class CouponActivity extends BaseActivity implements CouponView {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.coupon_listview)
    ListView couponListview;
    Intent intent;
    Bundle bundle;
    RestaurantInfo restaurantInfo;
    CouponPresenterImpl couponPresenter;
    CouponMsgAdapter couponMsgAdapter;
    LoginInfo loginInfo;
    @BindView(R.id.coupon_add)
    ImageView couponAdd;
    private List<CouponMsgInfo> couponInfoList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_coupon);
        ButterKnife.bind(this);
        initView();
        initEvent();
    }

    private void initEvent() {
        toolbar.setNavigationOnClickListener(view -> finish());
        couponListview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (isCg_has_storemanager(restaurantInfo) && isSuperManager(loginInfo)) {
                    toast("店长会去操作，您不用费心了");
                } else {
                    delMsg(i);
                }
                return false;
            }
        });
    }

    private void delMsg(int pos) {
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                context);
        // set dialog message
        alertDialogBuilder
                .setTitle("删除提示")
                .setMessage("你将删除本条促销信息")
                .setCancelable(false)
                .setPositiveButton("确定",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                couponPresenter.delUrl(RestaurantUrl.Httptitle + RestaurantUrl.Delcoupon
                                        + "promid=" + couponInfoList.get(pos).getId());

                            }
                        })
                .setNegativeButton("取消",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel();
                            }
                        });

        // create alert dialog
        AlertDialog alertDialog = alertDialogBuilder.create();

        // show it
        alertDialog.show();
    }

    private void initView() {
        setTitleBar(toolbar, "促销与优惠");
        intent = getIntent();
        bundle = intent.getExtras();
        loginInfo = DataSupport.findLast(LoginInfo.class);
        restaurantInfo = (RestaurantInfo) bundle.getSerializable(Constant.RESTAURANTSINFO);
        couponPresenter = new CouponPresenterImpl(this);
        initAdapter();
        addData();


    }

    private void initAdapter() {
        couponInfoList = new ArrayList<>();
        couponMsgAdapter = new CouponMsgAdapter(context, couponInfoList, R.layout.listview_coupon_item);
        couponListview.setAdapter(couponMsgAdapter);
    }

    private void addData() {
        couponPresenter.addData(RestaurantUrl.Httptitle + RestaurantUrl.Showcoupon
                + "cgid=" + restaurantInfo.getId());
    }

    @Override
    public void recRes(List<CouponMsgInfo> couponMsgInfos) {
        couponInfoList.clear();
        couponInfoList.addAll(couponMsgInfos);
        couponMsgAdapter.notifyDataSetChanged();
    }

    @Override
    public void delRes(int res) {
        if (res == 1) {
            toast("删除成功");
            addData();
        } else {
            toast("删除失败，请重试");

        }
    }

    @OnClick(R.id.coupon_add)
    public void onClick() {
        if (isCg_has_storemanager(restaurantInfo) && isSuperManager(loginInfo)) {
            Toast.makeText(getApplicationContext(), "店长会去操作，您不用费心了", Toast.LENGTH_SHORT).show();
        } else {
            if (couponInfoList.size() < 5) {
                // TODO: 2016/12/20
                intent.setClass(context, AddCouponActivity.class);
                startActivityForResult(intent, 1);

            } else {
                Toast.makeText(context, "促销信息最多允许5条", Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        addData();
    }

    @Override
    public void showLoading() {
        showLoadingDialog();
    }

    @Override
    public void dissLoading() {
        dissLoadingDialog();
    }
}
